﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Management;
using System.Net;
using System.Net.NetworkInformation;

namespace Jarvis.Sensors
{
    class NetworkSensors : Sensor
    {
        public NetworkSensors()
            : base(1000)
        {
            this.name = "Network Sensors";
        }

        public override void Sense(object stateInfo)
        {
            Sensor ai = new Sensor("Available Interfaces");
            Sensor ii = new Sensor("Inactive Interfaces");
            Sensor ip = new Sensor("Active IP Networks");

            List<object> activeInterfaces = new List<object>();
            List<object> inactiveInterfaces = new List<object>();
            NetworkInterface[] ifs = NetworkInterface.GetAllNetworkInterfaces();

            foreach (NetworkInterface ni in ifs)
            {
                if (ni.OperationalStatus == OperationalStatus.Up)
                    activeInterfaces.Add(ni.Name);
                else
                    inactiveInterfaces.Add(ni.Name);
            }

            ai.log(activeInterfaces);
            ii.log(inactiveInterfaces);

            ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 'TRUE'");
            ManagementObjectCollection queryCollection = query.Get();
            foreach (ManagementObject mo in queryCollection)
            {
                string[] ips = (string[])mo["IPAddress"];
                string[] subnets = (string[])mo["IPSubnet"];

                for (uint i = 0; i < ips.Length; i++)
                {
                    try
                    {
                        string ipString = ips[i];

                        IPAddress ipAddress = IPAddress.Parse(ips[i]);
                        IPAddress subnetAddress = IPAddress.Parse(subnets[i]);

                        byte[] ipBytes = ipAddress.GetAddressBytes();
                        byte[] subBytes = subnetAddress.GetAddressBytes();

                        for (int j = (subBytes.Length - 1); j > 0; j--)
                            ipBytes[j] &= subBytes[j];

                        IPAddress networkAddress = new IPAddress(ipBytes);

                        if (networkAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                        {
                            Sensor ipFour = new Sensor("IPv4 Network Address");
                            ipFour.log(networkAddress.ToString());
                        }
                        else
                        {
                            Sensor ipSix = new Sensor("IPv6 Network Address");
                            ipSix.log(networkAddress.ToString());
                        }
                    }
                    catch (FormatException)
                    {

                    }
                }
            }
        }
    }
}
